package com.chinaoly.sdk.view.http_test;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chinaoly.sdk.R;
import com.chinaoly.sdk.api.ApiService;
import com.chinaoly.sdk.base.BaseActivity;
import com.chinaoly.sdk.http.OlyHttp;
import com.chinaoly.sdk.util.LogUtils;
import com.chinaoly.sdk.util.ObservableUtil;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * @author wubin
 */
public class HttpTestActivity extends BaseActivity {




   /* @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IDataSource e;
        ApiService apiService = OlyHttp.newInstance().create(ApiService.class);
        IDataSource remote = new RemoteSource(apiService);
        IDataSource dataRepository = new DataRepository(remote);

        *//*dataRepository.a(1, 1, 1, new DataCallBackListener<BaseResult<String>>() {
            @Override
            public void onDataLoaded(BaseResult<String> stringBaseResult) {

            }

            @Override
            public void onDataNotAvailable(String msg) {

            }
        });
        dataRepository.abc("", 1, null, new DataCallBackListener<BaseResult<Long>>() {
            @Override
            public void onDataLoaded(BaseResult<Long> longBaseResult) {

            }

            @Override
            public void onDataNotAvailable(String msg) {

            }
        });*//*
    }*/

    String path;
    private TextView tv1;

    @Override
    public void initData(Bundle bundle) {

    }

    private void upload() {

        ApiService apiService = OlyHttp.newInstance().create(ApiService.class);
        // apiService.upload()
       // "multipart/form-data"


       // MultipartBody.Part p1 = MultipartBody.Part.createFormData("group", "151", b1);
       // MultipartBody.Part p2 = MultipartBody.Part.createFormData("destruct_interval", "06:00:00", b2);



        //"multipart/form-data"
        //"application/octet-stream"
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(null, file);
        RequestBody b1  =  RequestBody.create(null, "151");
        RequestBody b2  =  RequestBody.create(null, "06:00:00");
        MultipartBody.Part formData = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        Observable ob =   ObservableUtil.transform(apiService.upload(b1,b2,
                formData));
        ob.subscribe(new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                LogUtils.d("obj:::"+o);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtils.d("throwable::"+throwable);
            }
        });
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_http_test;
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {

    }

    @Override
    public void doBusiness() {
        ActivityCompat.requestPermissions(this, new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                }, 100


        );
        Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
        tv1 = findViewById(R.id.tv1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent,1);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upload();
            }
        });
    }

    @Override
    public void onWidgetclick(View view) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToFirst();
            path = actualimagecursor.getString(actual_image_column_index);
           // File file = new File(img_path);
            tv1.setText(path);
          //  Toast.makeText(HttpTestActivity.this, file.toString(), Toast.LENGTH_SHORT).show();
        }


    }
}
